<?php

/**
 * @property string $name
 * @property string $first_name
 * @property string $last_name
 * @property string $birthday
 * @property string $profile_url
 * @property string $username
 * @property string $email
 * @property integer $gender_id
 * @property integer $hometown_id
 * @property integer $location_id
 * @property integer $avatar_id
 * @property integer $search_purpose_id
 * @property string $last_login
 * @property string $fb_token
 *
 * @property GenderModel $gender
 * @property LocationModel $location
 * @property LocationModel $hometown
 * @property WorkModel[] $works
 * @property EducationModel[] $educations
 * @property PhotoModel $avatar
 * @property PhotoModel[] $photos
 */
class UserModel extends BaseModel
{
	public function tableName()
	{
		return '{{user}}';
	}

	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

    public function relations()
    {
        return array
        (
            'gender' => array(self::BELONGS_TO, 'GenderModel', 'gender_id'),
            'location' => array(self::BELONGS_TO, 'LocationModel', 'location_id'),
            'hometown' => array(self::BELONGS_TO, 'LocationModel', 'hometown_id'),
            'works' => array(self::MANY_MANY, 'WorkModel', '{{user_work}}(user_id,work_id)'),
            'educations' => array(self::MANY_MANY, 'EducationModel', '{{user_education}}(user_id,education_id)'),
            'avatar' => array(self::BELONGS_TO, 'PhotoModel', 'avatar_id'),
            'photos' => array(self::HAS_MANY, 'PhotoModel', 'user_id'),
        );
    }

    public function addWork($userId, $workId)
    {
        $sql = 'INSERT INTO {{user_work}} VALUES (NULL, :userId, :workId, NOW(), NOW())';
        Yii::app()->db->createCommand($sql)
                      ->bindParam(':userId', $userId)
                      ->bindParam(':workId', $workId)
                      ->execute();
    }
    
    public function deleteWorkByUserId($userId)
    {
        $sql = 'DELETE FROM {{user_work}} WHERE user_id = :userId';
        Yii::app()->db->createCommand($sql)
                      ->bindParam(':userId', $userId)
                      ->execute();
    }

    public function addEducation($userId, $educationId)
    {
        $sql = 'INSERT INTO {{user_education}} VALUES (NULL, :userId, :educationId, NOW(), NOW())';
        Yii::app()->db->createCommand($sql)
                      ->bindParam(':userId', $userId)
                      ->bindParam(':educationId', $educationId)
                      ->execute();
    }
    
    public function deleteEducationByUserId($userId)
    {
        $sql = 'DELETE FROM {{user_education}} WHERE user_id = :userId';
        Yii::app()->db->createCommand($sql)
                      ->bindParam(':userId', $userId)
                      ->execute();
    }

    public function getSnapshot($userId)
    {
        /*$sql = 'SELECT um.id
                FROM
                    oak_user u JOIN oak_snapshot_preference sp ON u.id = sp.id,
                    oak_user_mapping um
                WHERE
                    u.id = 1 AND
                    um.search_purpose_id = u.search_purpose_id AND
                    um.gender_id = sp.gender_id AND
                    um.year_of_birth BETWEEN YEAR(CURDATE()) - sp.age_to AND YEAR(CURDATE()) - sp.age_from AND
                    um.city_id = sp.city_id AND
                    um.distance <= sp.distance
                ORDER BY RAND()
                LIMIT 200';*/

        $sql = 'SELECT u.id
                FROM {{user}} u
                WHERE
                    u.id != :userId AND
                    NOT EXISTS
                    (
                        SELECT 1
                        FROM {{user_like}} l
                        WHERE
                            l.user_id = :userId AND
                            l.target_id = u.id
                    )
                ORDER BY RAND()
                LIMIT %d';
        $sql = sprintf($sql, CONST_SNAPSHOT_MAX_PER_DAY);
        return Yii::app()->db->createCommand($sql)
                         ->bindParam(':userId', $userId)
                         ->queryColumn();
    }
}
